// pages/order_detail/index.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id:0,
    order:{},
    payIdx: 1,
    showPop:false,
	config:{},
	cert:''
  },

  togglePop(){
    this.setData({
      showPop: !this.data.showPop
    })
  },
  payClick(e) {
    this.setData({
      payIdx: e.currentTarget.dataset.index
    })
  },
  choosePackageOrder(e){
	if(this.data.order.cert_up == 1){
		wx.showModal({
		  title: '提示',
		  content: '您已上传凭证，等待平台审核，确定更换支付',
		  complete: (res) => {
			if (res.confirm) {
				this.setData({
					payIdx: this.data.order.pay_type,
					showPop: !this.data.showPop
				  })
			}
		  }
		})
	}else{
		this.setData({
			payIdx: this.data.order.pay_type,
			showPop: !this.data.showPop
		  })
	}
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      order_id: options.order_id
    });
    let page = this;
    core.get({url: '/xilujob.common/corporate',loading:false,success(ret, data) {
      page.setData({
        config: ret.data
      });
    }
  });
    this.fetchOrderDetail();
  },

  fetchOrderDetail(){
    let page = this;
    core.get({url: '/xilujob.package_order/detail',data:{order_id:this.data.order_id},loading:false,success(ret, data) {
      page.setData({
        order: ret.data
      });
    }});
  },


   /**
   * 订单取消
   */
  orderCancel(event) {
    let page = this;
	let order = this.data.order;
	
	let content = order.cert_up == 1?'您已上传凭证，等待平台审核，确定取消订单':'是否取消支付？';
    wx.showModal({
      title: '提示',
      content: content,
      success: res => {
        if(res.confirm){
          core.post({
            url: '/xilujob.package_order/cancel', data: {package_order_id: order.id},
            success: ret => {
              page.fetchOrderDetail();
          }
          });
        }
      }
    })
  },

  /**
   * 套餐购买
   */
  onsubmit(){
    let packageOrder = this.data.order;
    let package_order_id = packageOrder.id;
    let config = this.data.config;
    //let ticket_type = this.data.ticketType;
    //let ticket = this.data.ticket;
    let pay_type = this.data.payIdx;
    //let remarks = this.data.remarks;
    let data = {
      platform: 'wxmini',
      order_id:package_order_id,
      //ticket_type:ticket_type,
      //ticket:ticket,
      pay_type:pay_type,
      corporate_company:config.corporate_company,
      open_bank:config.open_bank,
      corporate_account:config.corporate_account,
      //remarks:remarks
    }
    let page = this;
    if(pay_type == 1){
      wx.login({
        success(wxRes) {
          let code = wxRes.code;
          core.post({
            url: '/xilujob.user/wxlogin',
            data: {code: code},
            success: (ret, response) => {
              data['third_id'] = ret.data.third.third_id;
              page.payment(data);
            },fail: (ret, response) => {
              wx.showToast({
                title: '下单失败',
                icon:'none'
              })
            }
          });
        },
      });
    }else{
      page.payment(data);
    }
  },
  payment(order){
    let page = this;
    core.post({url: '/xilujob.pay/pay',data:order,loading:true,success(ret, data) {
      if(order.pay_type == 1){
          //发起支付
          wx.requestPayment({
            'timeStamp': ret.data.timeStamp,
            'nonceStr': ret.data.nonceStr,
            'package': ret.data.package,
            'signType': ret.data.signType,
            'paySign': ret.data.paySign,
            'success': function (res) {
              wx.showToast({
                icon: "none",
                title: '支付成功',
              })
              page.fetchOrderDetail();
              page.togglePop();
            },
            'fail': function (res) {
              wx.showToast({
                icon: "none",
                title: '支付失败',
              })
            },
            'complete': function (res) {
              
            }
          });
      }else{
        wx.showToast({
          title: ret.msg,
          icon:'none'
        })
      }
    }});
  },
  //付款凭证
  uploadCert(){
	  let page = this;
	wx.chooseMedia({
		count: 1,
		mediaType: ['image'],
		sourceType: ['album', 'camera'],
		success(res) {
			core.uploadFile({
				filePath: res.tempFiles[0].tempFilePath,
				success(ret, response) {
					page.setData({
						cert: ret.data.url
					})
				}
			  });
		}
	})
  },
  certUp(){
	let page = this;
	if(this.data.cert == ''){
		wx.showToast({title: '请先上传凭证',icon:'none'});
		return '';
	}
	core.post({
		url: '/xilujob.package_order/up_cert', data: {package_order_id: this.data.order_id,cert:this.data.cert},
		success: ret => {
		  page.fetchOrderDetail();
	  }
	  });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

})